/*=========================================================================
Elite College as Engines of Upward Mobility: Evidence from Colombia's Ser Pilo Paga
Authors: Juliana Londoño-Vélez, Catherine Rodriguez, Fabio Sánchez
and Luis Esteban Álvarez-Arango

Creation date: June 6th, 2025
--------------------------------------------------------------------------
Input for Figures 6b, A8b and a9b
=========================================================================*/


use "${data}/data_RD", clear
keep if icfes_per==20142
glo cond_saber11	"eligible_sisben==1"
glo cond_sisben		"eligible_saber11==1"

keep estu_snp eligible_* running_*
merge m:1 estu_snp using "${data2}/PILA_cuatrimestral", keep(1 3) nogen
rename ibc* wage*
rename trabajo* work*
rename sm* mw*

foreach x of varlist work_* wage_* mw_*{
	replace `x'=0 if `x'==.
}

foreach x of varlist wage*{
	local y=regexr("`x'","wage_","")
	gen ln_wage_`y'=ln(`x')
}
	
global outcomes_2 wage_201304 wage_201308 wage_201312 wage_201404 wage_201408 wage_201412 wage_201504 wage_201508 wage_201512 wage_201604 wage_201608 wage_201612 wage_201704 wage_201708 wage_201712 wage_201804 wage_201808 wage_201812 wage_201904 wage_201908 wage_201912 wage_202004 wage_202008 wage_202012 wage_202104 wage_202108 wage_202112 wage_202204 wage_202208 wage_202212 wage_202304 wage_202308 wage_202312 mw_201304 mw_201308 mw_201312 mw_201404 mw_201408 mw_201412 mw_201504 mw_201508 mw_201512 mw_201604 mw_201608 mw_201612 mw_201704 mw_201708 mw_201712 mw_201804 mw_201808 mw_201812 mw_201904 mw_201908 mw_201912 mw_202004 mw_202008 mw_202012 mw_202104 mw_202108 mw_202112 mw_202204 mw_202208 mw_202212 mw_202304 mw_202308 mw_202312 work_201304 work_201308 work_201312 work_201404 work_201408 work_201412 work_201504 work_201508 work_201512 work_201604 work_201608 work_201612 work_201704 work_201708 work_201712 work_201804 work_201808 work_201812 work_201904 work_201908 work_201912 work_202004 work_202008 work_202012 work_202104 work_202108 work_202112 work_202204 work_202208 work_202212 work_202304 work_202308 work_202312 ln_wage_201304 ln_wage_201308 ln_wage_201312 ln_wage_201404 ln_wage_201408 ln_wage_201412 ln_wage_201504 ln_wage_201508 ln_wage_201512 ln_wage_201604 ln_wage_201608 ln_wage_201612 ln_wage_201704 ln_wage_201708 ln_wage_201712 ln_wage_201804 ln_wage_201808 ln_wage_201812 ln_wage_201904 ln_wage_201908 ln_wage_201912 ln_wage_202004 ln_wage_202008 ln_wage_202012 ln_wage_202104 ln_wage_202108 ln_wage_202112 ln_wage_202204 ln_wage_202208 ln_wage_202212 ln_wage_202304 ln_wage_202308 ln_wage_202312

local NO: word count of $outcomes_2
local NO=`NO'-1
matrix ry2014=J(`NO',34,.)
local i=1
foreach o of global outcomes_2{
	foreach r in saber11 sisben{
	if "`r'"=="saber11" local j=0
	if "`r'"=="sisben" 	local j=17
	
	rdrobust `o' running_`r' if ${cond_`r'}, masspoints(off) 
	matrix ry2014[`i',`=`j'+1']=e(tau_cl)
	matrix ry2014[`i',`=`j'+2']=e(tau_bc)
	matrix ry2014[`i',`=`j'+3']=e(se_tau_cl)
	matrix ry2014[`i',`=`j'+4']=e(se_tau_rb)
	matrix ry2014[`i',`=`j'+5']=e(ci_r_rb)
	matrix ry2014[`i',`=`j'+6']=e(ci_l_rb)
	matrix ry2014[`i',`=`j'+7']=e(tau_cl_l)
	matrix ry2014[`i',`=`j'+8']=e(tau_bc_l)
	matrix ry2014[`i',`=`j'+9']=e(pv_cl)
	matrix ry2014[`i',`=`j'+10']=e(pv_rb)
	matrix ry2014[`i',`=`j'+11']=e(h_l)
	matrix ry2014[`i',`=`j'+12']=e(b_l)
	matrix ry2014[`i',`=`j'+13']=e(N_h_l)
	matrix ry2014[`i',`=`j'+14']=e(N_h_r)
	matrix ry2014[`i',`=`j'+15']=e(N_b_l)
	matrix ry2014[`i',`=`j'+16']=e(N_b_r)
	matrix ry2014[`i',`=`j'+17']=e(N)
	}
	local i=1+`i'
}


***********************************
** Construcción Tablas de resultados
***********************************	

clear
svmat ry2014
gen nombre=""
local NO: word count of $outcomes_2
local NO=`NO'-1
forvalues i=1/`NO'{
	local nn: word `i' of ${outcomes_2}
	replace nombre="`nn'" in `i'
}
order nombre
local nombres cc cr sec ser cs ci cc0 cr0 pc pr bwc bwr nec0 nec ner0 ner n
local nn: word count of `nombres'
local nn=`nn'-1
forvalues j=1/`nn'{
	local rn: word `j' of `nombres'
	rename ry2014`j' `rn'_11
	rename ry2014`=`j'+`nn'' `rn'_si
}

local runn 11 si
foreach x of local runn{
	label var cc_`x' "Coeficiente convencional"
	label var cr_`x' "Coeficiente robusto"
	label var sec_`x' "Error estándar convencional"
	label var ser_`x' "Error estándar robusto"
	label var cs_`x' "Intervalo superior robusto"
	label var ci_`x' "Intervalo inferior robusto"
	label var cc0_`x' "Media control convencional"
	label var cr0_`x' "Media control robusto"
	label var pc_`x' "Valor P convencional"
	label var pr_`x' "Valor P robusto"
	label var bwc_`x' "BW convencional"
	label var bwr_`x' "BW robusto"
	label var nec0_`x' "Observaciones efectivas izquierda convencional"
	label var nec_`x' "Observaciones efectivas derecha convencional"
	label var ner0_`x' "Observaciones efectivas izquierda robustas"
	label var ner_`x' "Observaciones efectivas derecha robustas"
	label var n_`x' "Observaciones"
}

save "${Intermediate}/Resultados_years_pila", replace
